package com.bj58.groupbuy.action.controllers;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.ServletRequestUtils;

import com.bj58.groupbuy.action.interceptors.Check;
import com.bj58.groupbuy.action.model.HomeHeatMap;
import com.bj58.groupbuy.action.service.DisplocalService;
import com.bj58.groupbuy.action.service.HeatMapService;
import com.bj58.groupbuy.action.service.PvMapService;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.action.util.ExcelUtil2;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext.Model;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.actionresult.ContentResult;
import com.bj58.wf.mvc.annotation.Path;


/**
 * @author lenovo
 *
 */
@Path("/heatmap")
public class HeatMapController extends MvcController {
	private DisplocalService displocalService = ServiceLocator.get(DisplocalService.class);
	private PvMapService pvMapService = ServiceLocator.get(PvMapService.class);
	private HeatMapService heatMapService = ServiceLocator.get(HeatMapService.class);
	
	@Path("heatMapAnalysis")
	@Check
	public ActionResult heatMap() {
		Model model= beat.getModel();
		String citypv="";
		String provincepv="";
		String procitypv="";
		List<String> cityname=new ArrayList<String>();
		try {
			String mapday =pvMapService.getMaxdate();
			citypv=pvMapService.getCityPcPv(mapday,"pc");
			provincepv=pvMapService.getProvincePcPv(mapday,"pc");
			procitypv=provincepv+citypv;
		    cityname=displocalService.getallcityname();
		    model.add("cityList",cityname);
		    model.add("filecity","北京");
		    model.add("startdate",mapday);
			model.add("type","pc");
			model.add("type1","pc");
			model.add("stat_type","day");
			model.add("selectpvtype","pc");
			model.add("selecttype","map");
			model.add("procitypv",procitypv);
			return ActionResult.view("/ors/heatMapAnalysis");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@Path("getPage")
	@Check("heatmap/heatMapAnalysis")
	public ActionResult getPage() {
		HttpServletRequest request = beat.getRequest();
		try {
			String type= ServletRequestUtils.getStringParameter(request,"selectpvtype");
			String stat_type= ServletRequestUtils.getStringParameter(request,"stat_type","day");
			String date = ServletRequestUtils.getStringParameter(request,"selectdate");//DateUtil.getStringDate(DateUtil.getDateBefore2(new Date(), 1),"yyyy-MM-dd")
			String cityname = ServletRequestUtils.getStringParameter(request,"selectedcityname");
			String citybh="";
			if(cityname.charAt(0)-'a'>=0&&cityname.charAt(0)-'z'<=0)
			{
				citybh=cityname;
			}else
			citybh=displocalService.getcityname(cityname);
			date = date.replaceAll("-", "");
			log.info(date + "-" + citybh);
			if(type.equals("pc")){
				if(stat_type.equals("month")){
					return ActionResult.view("/heatmap/" + date + "/month_" + citybh);
				}else{
					return ActionResult.view("/heatmap/" + date + "/" + citybh);
				}
			}else if(type.equals("m"))
			    return ActionResult.view("/heatmap/" + date + "/"+citybh+"_M");
			else
				return new ContentResult("This type is not exist!Only pc and m are allowed!");
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("/ors/heatMapAnalysis");
		}
	}
	@Path("getMapPage")
	@Check("heatmap/heatMapAnalysis")
	public ActionResult getMapPage() {
		HttpServletRequest request = beat.getRequest();
		Model model= beat.getModel();
		List<String> cityname=new ArrayList<String>();
		String citypv="";
		String provincepv="";
		String procitypv="";
		try {
			cityname=displocalService.getallcityname();
			String date = ServletRequestUtils.getStringParameter(request,"startdate", DateUtil.getStringDate(DateUtil.getDateBefore2(new Date(), 1),"yyyy-MM-dd"));
			String selecttype=ServletRequestUtils.getStringParameter(request,"selecttype");
			if(selecttype.equals("map"))
			{
				String type= ServletRequestUtils.getStringParameter(request,"type");
				String mapday =date;
				citypv=pvMapService.getCityPcPv(mapday,type);
				provincepv=pvMapService.getProvincePcPv(mapday,type);
				procitypv=provincepv+citypv;
			    model.add("cityList",cityname);
			    model.add("filecity","北京");
			    model.add("startdate",mapday);
				model.add("type",type);
				model.add("type1","pc");
				model.add("stat_type","day");
				model.add("selectpvtype","pc");
				model.add("selecttype","map");
				model.add("procitypv",procitypv);
				return ActionResult.view("/ors/heatMapAnalysis");
			}else
			{
				String type1= ServletRequestUtils.getStringParameter(request,"type1");
				String cityn = ServletRequestUtils.getStringParameter(request,"filecity");
				String stat_type= ServletRequestUtils.getStringParameter(request,"stat_type","day");
				String citybh="";
				citybh=displocalService.getcityname(cityn);
				date = date.replaceAll("-", "");
				log.info(date + "-" + citybh);
				model.add("cityList",cityname);
				if(type1.equals("pc")){
					if(stat_type.endsWith("month")){
						return ActionResult.view("/heatmap/" + date + "/month_" + citybh);
					}else{
						return ActionResult.view("/heatmap/" + date + "/" + citybh);
					}
				}
				return ActionResult.view("/heatmap/" + date + "/"+citybh+"_M");
			
				
			}
			
		
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("/ors/heatMapAnalysis");
		}
	}
	
	
	
	
	//改版测试
	@Path("heatMapTest")
	@Check
	public ActionResult heatMapTest() {
		Model model= beat.getModel();
		String citypv="";
		String provincepv="";
		String procitypv="";
		List<String> cityname=new ArrayList<String>();
		try {
			String mapday =pvMapService.getMaxdate();
			citypv=pvMapService.getCityPcPv(mapday,"pc");
			provincepv=pvMapService.getProvincePcPv(mapday,"pc");
			procitypv=provincepv+citypv;
		    cityname=displocalService.getallcityname();
		    model.add("cityList",cityname);
		    model.add("filecity","北京");
		    model.add("startdate",mapday);
			model.add("type","m");
			model.add("type1","m");
			model.add("selectpvtype","m");
			model.add("selecttype","page");
			model.add("procitypv",procitypv);
			return ActionResult.view("/ors/heatMapAnalysis_Test");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@Path("downloadData")
	@Check("heatmap/heatMapAnalysis")
	public ActionResult homePageDataDownload() {
		HttpServletRequest request = beat.getRequest();
		try {
			String stat_date = ServletRequestUtils.getStringParameter(request,"startdate", DateUtil.getStringDate(DateUtil.getDateBefore2(new Date(), 1),"yyyy-MM-dd"));
		
			String type1= ServletRequestUtils.getStringParameter(request,"type1","");
			String cityn = ServletRequestUtils.getStringParameter(request,"filecity","");
			String citybh="";
			citybh=displocalService.getcityname(cityn);
			log.info(" Downloading  heatmap data : "+ stat_date + "-" + cityn);
			
			List<HomeHeatMap> rs = null;
			if(type1.equals("pc")){
				rs = heatMapService.getCityDataList(stat_date, citybh, "PC");
			}
			else if("m".equals(type1)){
				rs = heatMapService.getCityDataList(stat_date, citybh, "M");
			}
			 ExcelUtil2<HomeHeatMap> u = new ExcelUtil2<HomeHeatMap>(HomeHeatMap.class);
			 u.createExcelFileAndDownload(rs, "城市首页热力图数据-"+cityn+"-"+stat_date);
			 return new ContentResult("暂无当日数据!");
		
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("/ors/heatMapAnalysis_Test");
		}
	}
	
}
